/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Server.DAO;

import DTO.CTPhieuDichVu_DTO;
import java.sql.ResultSet;

/**
 *
 * @author PEKEM
 */
public class CTPhieuDichVu_DAO {

    private static String sqlString;
    private static MSSQLConnect cn;
    private static ResultSet rs;

    public static int Insert(CTPhieuDichVu_DTO ct) throws Exception {
        int n = 0;
        sqlString = "INSERT INTO CTPHIEUDICHVU VALUES ('"
                + ct.getMaCTPhieuDichVu() + "','"
                + ct.getMaPhieuDichVu() + "','"
                + ct.getMaDichVu() + "','"
                + ct.getThanhTien() + "')";
        try {
            cn = new MSSQLConnect();
            n = cn.executeUpdate(sqlString);
        } catch (Exception e) {
            //System.out.println(e.toString());
            throw new Exception("Lỗi cơ sở dữ liệu");
        } finally {
            cn.Close();
        }
        return n;
    }

    public static int Delete(String maCTPhieuDichVu) throws Exception {
        int n;
        sqlString = "DELETE FROM CTPHIEUDICHVU WHERE MaCTPhieuDichVu='" + maCTPhieuDichVu + "'";
        try {
            cn = new MSSQLConnect();
            //Thực thi câu lệnh SQL
            n = cn.executeUpdate(sqlString);

        } catch (Exception e) {
            throw new Exception("Lỗi Kết Nối Cơ sở dữ liệu.");
        } finally {
            cn.Close();
        }
        return n;
    }

    public static int Update(CTPhieuDichVu_DTO ct) throws Exception {
        int n;
        sqlString = "UPDATE CTPHIEUDICHVU SET MaPhieuDichVu='" + ct.getMaPhieuDichVu()
                + "',MaDichVu'" + ct.getMaDichVu()
                + "',ThanhTien'" + ct.getThanhTien()
                + "' WHERE MaCTPhieuDichVu='" + ct.getMaCTPhieuDichVu() + "'";
        try {
            cn = new MSSQLConnect();
            //Thực thi câu lệnh SQL
            n = cn.executeUpdate(sqlString);

        } catch (Exception e) {
            throw new Exception("Lỗi Kết Nối Cơ sở dữ liệu.");
        } finally {
            cn.Close();
        }
        return n;
    }

    public static String layMaCuoiCung() throws Exception {
        String kq;
        kq = null;
        sqlString = "SELECT TOP 1 MaCTPhieuDichVu as [MaCT] FROM CTPHIEUDICHVU ORDER BY MaCTPhieuDichVu DESC";
        try {
            //thực hiện câu truy vấn
            cn = new MSSQLConnect();
            //thực hiện câu truy vấn
            rs = cn.excuteQuery(sqlString);
            while (rs.next()) {
                kq = rs.getString(1);
            }

        } catch (Exception e) {
            throw new Exception("Không thể lấy mã max");
        } finally {
            cn.Close();
        }
        return kq;
    }

    public static String taoMaTuDong(String maGoc, int batDau) throws Exception {
        String str, kq;
        int so;
        try {
            str = maGoc.substring(batDau);
            kq = maGoc.substring(0, batDau);
            so = Integer.parseInt(str) + 1;
            for (int i = 0; i < str.length() - Integer.valueOf(so).toString().length(); i++) {
                kq += "0";
                //return kq;
            }
            kq = kq + so;
        } catch (Exception x) {
            return null;
        }
        return kq;
    }
}
